package view
{
	/**
	 *
	 *@author CrazyChen 
	 *2013-4-11
	 */	
	import data.Globals;
	import data.PlantProp;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.utils.getDefinitionByName;
	
	import manager.AssetManage;
	import manager.ObjDataManage;
	import manager.ObjectPool;
	
	import view.base.ClearView;
	import view.plant.PlantItem;
	
	public class PlantView extends ClearView
	{
		public var ary:Array;
		
		public var targetPlant:PlantItem;
		public function PlantView()
		{
			super();
			ary= [];
			this.addEventListener(MouseEvent.CLICK,selectPlant);
			this.addEventListener(MouseEvent.MOUSE_OVER,over);
			this.addEventListener(MouseEvent.MOUSE_OUT,out);
		}
		
		protected function out(event:MouseEvent):void
		{
			Globals.gameView.showPlantTip(false);
		}
		
		protected function over(event:MouseEvent):void
		{
			var plant:PlantItem = event.target as PlantItem;
			if(plant){		
				Globals.gameView.showPlantTip(true,plant.energy,plant.blood,plant.x+5,plant.y);
			}
		}
		
		protected function selectPlant(event:MouseEvent):void
		{
			targetPlant  = event.target as PlantItem;
			targetPlant.showBloodBar = true;
		}
		
		public function initface():void{
			if(!AssetManage.plantMap){
				AssetManage.loaderPlant();
				return;
			}
		
			var plantAry:Array = ObjDataManage.getInstance().plantAry;
			var plantProp:PlantProp;
			var len:int = plantAry.length;
			for(var i:int=0;i<len;i++){
				plantProp = plantAry[i];
				var o:Object = ObjDataManage.getInstance().plantInfo[plantProp.name];
				var plant:PlantItem = ObjectPool.getObj(view.plant.PlantItem) as PlantItem;
				this.addChild(plant);
				ary.push(plant);
				plant.x = plantProp.x;
				plant.y = plantProp.y;
				plant.maxHp =o.hp;
				plant.maxEnergy = o.energy;
				plant.gradAry = plantProp.gradAry;
				plant.energy = o.energy;
				plant.bmd(AssetManage.plantMap["plant"+plantProp.name].bitmapData,plantProp.wid,plantProp.height);
				plant.onRemove = onRemove;
				plant.showBloodBar = false;
				var p:Point;
				for(var j:int=0;j<plantProp.gradAry.length;j++){
					p = plantProp.gradAry[j];
					Globals.gameView.setGradIsCanSite(p.x,p.y,false);
				}
			
			}
			
		}
		
		private function onRemove(plant:PlantItem):void{
			ary.splice(ary.indexOf(plant),1);
			plant.showBloodBar = false;
			if(plant == targetPlant)targetPlant = null;
		}
	}
}